package com.apts.model;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.apts.constant.Constant_PlanB_;
import com.apts.controller.Controller_PlanB_;
import com.apts.jfinalbase.BaseModel;
import com.apts.util.FileUtil;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class TbEnterprise extends BaseModel<TbEnterprise> {
	public static Logger log4j = Logger.getLogger(Controller_PlanB_.class);
	private static final long serialVersionUID = -5310102820916603178L;
	public static final TbEnterprise dao = new TbEnterprise();

	// 查询企业信息(查询所有企业的信息)
	public List<TbEnterprise> findAll() {
//		String sql = "select * from tb_enterprise LIMIT '" + start + "','" + end + "'";
		String sql2 = "select * from tb_enterprise order by id desc";
		List<TbEnterprise> list = TbEnterprise.dao.find(sql2);
		return list;
	}

	// 查询企业信息(按照id查询)
	public TbEnterprise findById(String id) {
		String sql = "select * from tb_enterprise where id = ? ";
		TbEnterprise enterprise = TbEnterprise.dao.findFirst(sql, id);
		return enterprise;
	}

	// 根据企业编号模糊查找
	public List<TbEnterprise> findbyregcode(String regcode) {
		String sql = "select * from tb_enterprise where regcode like '%" + regcode + "%' ";
		List<TbEnterprise> list = TbEnterprise.dao.find(sql);
		return list;
	}

	// 根据企业名模糊查找
	public List<TbEnterprise> findbyname(String epname) {
		String name = epname;
		System.out.print("name:" + name);
		String sql = "select * from tb_enterprise where epname like '%" + epname + "%' ";
		List<TbEnterprise> list = TbEnterprise.dao.find(sql);
		return list;
	}

	// 根据企业地址模糊查找
	public List<TbEnterprise> findbyaddress(String address) {
		String sql = "select * from tb_enterprise where address like '%" + address + "%' ";
		List<TbEnterprise> list = TbEnterprise.dao.find(sql);
		return list;
	}

	// 根据企业经营范围模糊查找
	public List<TbEnterprise> findbyrunscope(String runscope) {
		String sql = "select * from tb_enterprise where runscope like '%" + runscope + "%' ";
		List<TbEnterprise> list = TbEnterprise.dao.find(sql);
		return list;
	}

	// 根据企业法人模糊查找
	public List<TbEnterprise> findbylegalperson(String legalperson) {
		String sql = "select * from tb_enterprise where legalperson like '%" + legalperson + "%' ";
		List<TbEnterprise> list = TbEnterprise.dao.find(sql);
		return list;
	}

	// 增加企业信息
	public void insert(Map<String, Object> map) {

		System.out.println("进入insert");
		String epname = (String) map.get("epname");
		String regcode = (String) map.get("regcode");
		String address = (String) map.get("address");
		String runscope = (String) map.get("runscope");
		String mobiletele = (String) map.get("mobiletele");
		String cellphone = (String) map.get("cellphone");
		String isdeleted = "0";
		String regtime = (String) map.get("regtime");
		String legalperson = (String) map.get("legalperson");
		String intruduction = (String) map.get("intruduction");
		String logo = (String) map.get("logo");

//		System.out.println("legalperson是：" + legalperson);

//		int legalpersonint = Integer.parseInt(isdeleted);

		TbEnterprise enterprise = new TbEnterprise();

		enterprise.set("epname", epname).set("regcode", regcode).set("address", address).set("runscope", runscope)
				.set("mobiletele", mobiletele).set("cellphone", cellphone).set("isdeleted", isdeleted).set("regtime", regtime)
				.set("legalperson", legalperson).set("intruduction", intruduction).set("logo", logo).save();

		System.out.println("完成插入");

	}

	/// **********企业信息更新*****企业信息更新********企业信息更新********
	public void update(Map<String, Object> map) {
		System.out.println("这里是update");
//		int legalpersonint = 0;
		String id = (String)map.get("id");
		String regcode = (String) map.get("regcode");
		String epname = (String) map.get("epname");
		String address = (String) map.get("address");
		String runscope = (String) map.get("runscope");
		String mobiletele = (String) map.get("mobiletele");
		String cellphone = (String) map.get("cellphone");
		String legalperson = (String) map.get("legalperson");
		String intruduction = (String) map.get("intruduction");
		String logo = (String) map.get("logo");

		Record enterprise = Db.findById("tb_enterprise", id)
				.set("regcode", regcode).set("epname", epname).set("cellphone", cellphone)
				.set("legalperson", legalperson).set("intruduction", intruduction)
				.set("address", address).set("runscope", runscope).set("mobiletele", mobiletele);
		if( logo != null ){
			enterprise.set("logo", logo);
		}
		Db.update("tb_enterprise", enterprise);
		
		/*TbEnterprise enterprise = new TbEnterprise();

		System.out.println("regcode:" + map.get("regcode"));

		enterprise = enterprise.findById(regcode);

		if (epname == "") {
			epname = enterprise.getStr("epname");
			System.out.println("epname:" + epname);
		}

		if (address == "") {
			address = enterprise.getStr("address");
			System.out.println("address:" + address);
		}

		if (runscope == "") {
			runscope = enterprise.getStr("runscope");
			System.out.println("runscope:" + runscope);
		}

		if (mobiletele == "") {
			mobiletele = enterprise.getStr("mobiletele");
			System.out.println("mobiletele:" + mobiletele);
		}

		if (cellphone == "") {
			cellphone = enterprise.getStr("cellphone");
			System.out.println("cellphone:" + cellphone);
		}

		if (isdeleted == "") {
			isdeleted = enterprise.getStr("isdeleted");
			legalpersonint = Integer.parseInt(isdeleted);
			System.out.println("isdeleted:" + isdeleted);
		}

		if (legalperson == "") {
			legalperson = enterprise.getStr("legalperson");
			System.out.println("legalperson:" + legalperson);
		}

		if (intruduction == "") {
			intruduction = enterprise.getStr("intruduction");
			System.out.println("legalperson:" + intruduction);
		}

		if (logo == "") {
			logo = enterprise.getStr("logo");
			System.out.println("logo:" + logo);
		}

		enterprise.set("epname", epname).set("regcode", regcode).set("address", address).set("runscope", runscope)
				.set("mobiletele", mobiletele).set("cellphone", cellphone).set("isdeleted", isdeleted)
				.set("legalperson", legalpersonint).set("intruduction", intruduction).set("logo", logo).update();
		System.out.println("完成update");*/
	}

	// **********企业删除删除*****企业删除删除********企业删除删除********
	public void Delete(String id) {
		// 删除图片
		Record te = Db.findById("tb_enterprise", id);
		String logo = te.get("logo");
		logo = JFinal.me().getServletContext().getRealPath("/") + logo;
		log4j.info( "执行到这里：删除的logo的位置-->" + logo );
		FileUtil.deleteFile(logo);
		String regcode = te.get("regcode");
		regcode = JFinal.me().getServletContext().getRealPath("/") + File.separator + Constant_PlanB_.EditorfileResource + File.separator + regcode;
		FileUtil.DeleteFolder( regcode );
		Db.update( "DELETE FROM tb_enterprise WHERE id = ? ", id );
	}

}
